package bank

var (
	//用一个容量只有1的channel来保证最多只有一个goroutine在同一时刻访问一个共享变量
	lockChannel = make(chan struct{}, 1)
	balance     int
)

func Add(amount int) {
	lockChannel <- struct{}{}
	balance = balance + amount
	<-lockChannel
}

func Balance() int {
	lockChannel <- struct{}{}
	b := balance
	<-lockChannel
	return b
}

func Init() {
	lockChannel <- struct{}{}
	balance = 0
	<-lockChannel
}
